import 'package:common_utils/common_utils.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:xinxuan/common/appbar_util.dart';
import 'package:xinxuan/common/button_util.dart';
import 'package:xinxuan/common/scroll_configuration_common.dart';
import 'package:xinxuan/controllers/common_page_controller.dart';
import 'package:xinxuan/controllers/cooperation_controller/cooperation_page_controller.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:xinxuan/router/app_routers.dart';
import 'package:x_common_utils/utils/inkWell_util.dart';
import 'package:xinxuan/utils/color_util.dart';

class CooperationPage extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: pageBgColor,
      appBar: AppBarUtil(
        title: '邀请好友',
        showLeading: false,
        backgroundColor: pageBgColor,
      ),
      body: ScrollConfigurationCommon(child: ListView(
        children: [
          // Container(
          //   color: bgWhiteColor,
          //   child: Column(
          //     children: [
          //       Image.asset(
          //         'assets/images/cooperation_bg.png',
          //         width: 375.w,
          //         height: 217.w,
          //         fit: BoxFit.cover,
          //       ),
          //       SizedBox(height: 16.h),
          //       _invitationMsg(),
          //     ],
          //   ),
          // ),
          Container(
            color: bgWhiteColor,
            child: Column(
              children: [
                Container(
                  height: 44.h,
                  alignment: Alignment.center,
                  decoration: BoxDecoration(
                    border: Border(
                      bottom: BorderSide(
                        width: .5,
                        color: lineColor
                      )
                    )
                  ),
                  child: Image.asset(
                    'assets/icons/icon_Invite_friends.png',
                    width: 144.w,
                    height: 20.h,
                  ),
                ),
                Container(
                  padding: EdgeInsets.symmetric(horizontal: 16.w),
                  child: Container(
                    decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(5.w),
                      color: bgWhiteColor
                    ),
                    child: Row(
                      children: [
                        GetBuilder<CooperationPageController>(
                          init: CooperationPageController(),
                          builder: (controller) {
                            return _extensionItem(
                              path: 'assets/icons/icon_invitation.png', 
                              title: '邀请码加入', 
                              showBorder: false,
                              onTap: () {
                                controller.joinTeam();
                              }
                            );
                          },
                        ),
                        GetBuilder<CommonPageController>(
                          builder: (controller) {
                            return _extensionItem(
                              path: 'assets/icons/icon_link.png', 
                              title: '推广链接', 
                              showBorder: false,
                              onTap: () {
                                if (!TextUtil.isEmpty(controller.userData.value.invitationUrl)) {
                                  Clipboard.setData(ClipboardData(text: controller.userData.value.invitationUrl));
                                  EasyLoading.showToast('复制成功');
                                }
                              }
                            );
                          },
                        )
                      ],
                    ),
                  ),
                )
              ],
            ),
          ),
          Container(
            color: bgWhiteColor,
            margin: EdgeInsets.only(top: 12.h),
            child: Column(
              children: [
                Container(
                  height: 44.h,
                  alignment: Alignment.center,
                  decoration: BoxDecoration(
                    border: Border(
                      bottom: BorderSide(
                        width: .5,
                        color: lineColor
                      )
                    )
                  ),
                  child: Image.asset(
                    'assets/icons/icon_invite_link.png',
                    width: 144.w,
                    height: 20.h,
                  ),
                ),
                _invitationMsg()
              ],
            ),
          )
        ],
      )),
    );
  }

  /// 邀请信息
  Widget _invitationMsg() {
    return Container(
      width: 375.w,
      padding: EdgeInsets.symmetric(horizontal: 12.w),
      child: Container(
        child: GetBuilder<CommonPageController>(
          builder: (controller) {
            return Column(
              children: [
                _invitationItem('邀请码:', '${controller.userData.value.invitationCode}'),
                SizedBox(height: 8.h),
                _invitationItem('邀请链接:', '${controller.userData.value.invitationUrl}'),
                SizedBox(height: 16.h),
                Container(
                  padding: EdgeInsets.symmetric(horizontal: 45.w),
                  child: ButtonUtil(
                    text: '海报邀请', 
                    fontSize: 14.sp, 
                    borderRadius: 18.w, 
                    height: 36.h, 
                    onTop: () {
                      Get.toNamed('${AppRouters.COMMON_PAGE}${AppRouters.INVITATION_POSTER_PAGE}');
                    }
                  ),
                ),
                SizedBox(height: 16.h),
              ],
            );
          },
        ),
      ),
    );
  }

  /// 邀请码、邀请链接
  Widget _invitationItem(String title, String value) {
    return Container(
      // height: 34.h,
      margin: EdgeInsets.only(top: 16.h),
      padding: EdgeInsets.symmetric(horizontal: 12.w),
      // decoration: BoxDecoration(
      //   borderRadius: BorderRadius.circular(5.w),
      //   border: Border.all(
      //     width: .5,
      //     color: lineColor
      //   )
      // ),
      child: Row(
        children: [
          Container(
            width: 64.w,
            child: Text(
              title,
              style: TextStyle(
                color: textSubtitleColor,
                fontSize: 14.sp
              ),
            ),
          ),
          Expanded(child: Container(
            padding: EdgeInsets.symmetric(horizontal: 6.w),
            child: Text(
              value,
              textAlign: TextAlign.end,
              overflow: TextOverflow.ellipsis,
              maxLines: 1,
              style: TextStyle(
                color: textBlackColor,
                fontSize: 16.sp
              ),
            ),
          )),
          SizedBox(width: 12.w),
          InkWellUtil.shakeProofInkWell(Text(
            '复制',
            style: TextStyle(
              color: mainTextColor,
              fontSize: 12.sp
            ),
          ), () {
            if (!TextUtil.isEmpty(value)) {
              Clipboard.setData(ClipboardData(text: value));
              EasyLoading.showToast('复制成功');
            }
          })
        ],
      ),
    );
  }

  /// 邀请好友Item
  Widget _extensionItem({required String path, required String title, required Function onTap, bool showBorder = true}) {
    return InkWellUtil.shakeProofInkWell(Container(
      padding: EdgeInsets.symmetric(vertical: 16.h),
      width: 171.w,
      alignment: Alignment.center,
      decoration: BoxDecoration(
        border: Border(
          right: showBorder ? BorderSide(
            width: 1.w,
            color: lineColor
          ) : BorderSide.none
        )
      ),
      child: Column(
        children: [
          Image.asset(
            path,
            width: 48.w,
            height: 48.w,
          ),
          SizedBox(height: 4.h),
          Text(
            title,
            style: TextStyle(
              color: textBlackColor,
              fontSize: 12.sp
            ),
          )
        ],
      ),
    ), () => onTap());
  }

}